using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace GenerateP4SpecForm.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class SpecWriterTest
    {
        public SpecWriterTest() { }

        [TestMethod]
        public void WriteP4SpecForm()
        {
            //test options to write:
            string[] options =  new string[] { "option1: value1", "option2: value2" };

            //generate the form in a temp file:
            string tempPath = this.WriteSpecForm(options);

            //check file exists, and contains each option in a separate line:
            Assert.IsTrue(File.Exists(tempPath));
            string specText = File.ReadAllText(tempPath);
            Assert.IsFalse(string.IsNullOrEmpty(specText));
            foreach (string option in options)
            {
                Assert.IsTrue(specText.Contains(string.Format("{0}{1}", option, Environment.NewLine)));
            }

            File.Delete(tempPath);
        }

        [TestMethod]
        public void UpdateP4SpecForm()
        {
            //test options to write:
            string[] options = new string[] { "option1: value1", "option2: value2" };

            //generate the form in a temp file:
            string tempPath = this.WriteSpecForm(options);

            //update it, changing value1:
            SpecWriter.UpdateP4SpecForm(tempPath, "value1", "horse");

            //check file exists, and contains updated option:
            Assert.IsTrue(File.Exists(tempPath));
            string specText = File.ReadAllText(tempPath);
            Assert.IsFalse(string.IsNullOrEmpty(specText));
            Assert.IsFalse(specText.Contains(string.Format("option1: value1{0}", Environment.NewLine)));
            Assert.IsTrue(specText.Contains(string.Format("option1: horse{0}", Environment.NewLine)));

            File.Delete(tempPath);
        }

        [TestMethod]
        public void AppendToP4SpecForm()
        {
            //test options to write:
            string[] options = new string[] { "option1: value1", "option2: value2\r\n\r\n" };

            //generate the form in a temp file:
            string tempPath = this.WriteSpecForm(options);

            //append a new value to it:
            SpecWriter.AppendToP4SpecForm(tempPath, "option3: value3");

            //check file exists, and contains updated option:
            Assert.IsTrue(File.Exists(tempPath));
            string specText = File.ReadAllText(tempPath);
            Assert.IsFalse(string.IsNullOrEmpty(specText));
            Assert.IsTrue(specText.Contains(string.Format("option1: value1{0}", Environment.NewLine)));
            Assert.IsTrue(specText.Contains(string.Format("option2: value2{0}", Environment.NewLine)));
            Assert.IsTrue(specText.Contains(string.Format("option3: value3{0}", Environment.NewLine)));

            File.Delete(tempPath);
        }

        private string WriteSpecForm(string[] options)
        {
            //generate the form in a temp file:
            string tempPath = Path.GetTempFileName();
            SpecWriter.WriteP4SpecForm(tempPath, options);
            return tempPath;
        }
    }
}
